import java.util.Scanner;

/*
5
QWER 666
QWER
1234
qwe
666
QWER
 */
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
//        int leng = in.nextInt();
//        in.nextLine();
//        String str1 = in.next();
//        String str2 = in.nextLine();
//        String[] strs = new String[leng];
//        for (int i = 0; i < leng; i++) {
//            strs[i] = in.nextLine();
//        }
        int leng = 5;
        String str1 = "QWER";
        String str2 = "666";
        String[] strs = {"QWER","1234","qwe","666","QWER"};
        int min = -1;
        int left = -1;
        int right = -1;
        for(int i = 0; i < leng; i++) {
            if(strs[i].equals(str1)) {
                left = i;
            }else if(strs[i].equals(str2)) {
                right = i;
            }
            if(left != -1 && right != -1) {
                int tmp = 0;
                if(left < right) {
                    tmp = right - left;
                }else {
                    tmp = left - right;
                }
                if(min == -1 || tmp < min) {
                    min = tmp;
                }
            }
        }
        System.out.println(min);
    }
}